/**
 * @description 商品类别JS
 * @author 赵子靖
 * @since 2017-12-06
 */
var ShopType = function() {
    var t = {
        path :'',
        init : function() {
            t.initView();
            t.initEvent();
        },
        initView : function() {
            
        },
        initEvent : function() {
            
        },
        //加载数据
        render:function(){
            $("#shop-type-table").bootstrapTable({
                queryParams : function(params) {
                    $.extend(params, {
                    })
                    return params;
                }

            });
        },
        //添加
        addShopTypeDialog:function(parentId,parentName){
        $("#dialog").load(t.path+"/busShopType/forwardAddShopType.do",function(responseTxt,statusTxt,xhr){
            if(statusTxt=="success"){
                $('#add-shop-type-dialog').modal();
                $("#add-shop-type-dialog #stParentId").val(parentId);
                $("#add-shop-type-dialog #stParentName").val(parentName);
            }else if(statusTxt=="error"){
                alert("Error: "+xhr.status+": "+xhr.statusText);
            }
        }); 
        },
        //保存
        addShopType:function(){
            var stParentId = $("#add-shop-type-dialog #stParentId").val();
            var stName = $("#add-shop-type-dialog #stName").val();
            var stStatus = $("input[name='stStatus']:checked").val();
            var stSort = $("#add-shop-type-dialog #stSort").val();
            if(Util.isEmpty(stName)){
                $("#stNameSpan").html("请输入商品类别名称。");
                $("#add-shop-type-dialog #stName").focus();
                return;
            }
            $("#stNameSpan").html("");
            
            $.ajaxFileUpload({
                url:t.path+"/busShopType/addShopType.do",
                secureuri:false,
                fileElementId:["stIcon"],
                dataType:"json",
                cache:false,
                data:{
                    stParentId:stParentId,
                    stName:stName,
                    stStatus:stStatus,
                    stSort:stSort
                },
                success:function(data,status){//服务器响应成功处理
                    if(data.success){
                        alert("保存成功!");
                        $("#add-shop-type-dialog").modal('toggle');
                        $("#shop-type-table").bootstrapTable('refresh');
                    }else{
                        alert("保存失败!");
                    }
                },
                error:function(data,status,e){//服务器响应失败处理
                    alert(e);   
                }
            });
        },
        //编辑节点
        updateShopTypeDialog:function(id){
            $("#dialog").load(t.path+"/busShopType/forwardUpdateShopType.do?id="+id,function(responseTxt,statusTxt,xhr){
                if(statusTxt=="success"){
                    $('#update-shop-type-dialog').modal().css({'display':'table','width':'100%','height':'100%'});
                }else if(statusTxt=="error"){
                    alert("Error: "+xhr.status+": "+xhr.statusText);
                }
            }); 
        },
        //修改
        updateShopType:function(){
            var stId = $("#update-shop-type-dialog #stId").val();
            var stParentId = $("#update-shop-type-dialog #stParentId").val();
            var stName = $("#update-shop-type-dialog #stName").val();
            var stStatus = $("input[name='stStatus']:checked").val();
            var stSort = $("#update-shop-type-dialog #stSort").val();
            if(Util.isEmpty(stName)){
                $("#stNameSpan").html("请输入类别名称。");
                $("#update-shop-type-dialog #stName").focus();
                return;
            }
            $("#stNameSpan").html("");
            $.ajaxFileUpload({
                url:t.path+"/busShopType/updateShopType.do",
                secureuri:false,
                fileElementId:["stIcon"],
                dataType:"json",
                cache:false,
                data:{
                    stId:stId,
                    stParentId:stParentId,
                    stName:stName,
                    stStatus:stStatus,
                    stSort:stSort
                },
                success:function(data,status){//服务器响应成功处理
                    if(data.success){
                        alert("保存成功!");
                        $("#update-shop-type-dialog").modal('toggle');
                        $("#shop-type-table").bootstrapTable('refresh');
                    }else{
                        alert("保存失败!");
                    }
                },
                error:function(data,status,e){//服务器响应失败处理
                    alert(e);   
                }
            });
        },
        //删除节点
        deleteShopTypeDialog:function(id){
            bootbox.confirm({
                size : 'small',
                message : "确认删除该信息，删除后将不可恢复？",
                callback : function(result) {
                    if (result) {
                        $.ajax({
                            type:"POST",
                            url:t.path+"/busShopType/deleteShopType.do",
                            dataType:"json",
                            data:{
                                id:id
                            },
                            async: false,
                            cache:false,
                            success:function(data){
                                if (data && data.success) {
                                    alert("删除成功。");
                                    $("#shop-type-table").bootstrapTable('refresh');
                                } else {
                                    alert("删除失败.");
                                }
                            }
                        });
                    }
                }
            });
        },
        statusFormatter:function(value,row){
            if(value==1){
                return '正常';
            }else{
                return "<font style='color:red;'>停用</font>";
            }
        },
        fileFormatter:function(value,row){
            if(!Util.isEmpty(value)){
                return "<a href='"+t.path+"/upload/"+value+"'>"+value+"</a>";
            }
        },
        dateFormatter:function(value,row){
            if(!Util.isEmpty(value)){
                return value.substring(0,10);
            }
        },
        urlFormatter:function(value,row){
            if(!Util.isEmpty(value)){
                return "<a href='"+value+"' target='_blank'>"+value+"</a>";
            }
        },
        interfaceNumFormatter:function(value,row){
            return ' <a href="javascript:ShopTypeInterface.loadShopTypeInterfaceDialog(\''+row.hjlIsId+'\')">'+value+' </a>';
        },
        iconFormatter:function(value,row){
            return '<i class="'+value+'"></i>';
        },
        //子菜单
        detailFormatter: function(index, row) {
            var html = "<div style='width:90%; margin:auto;'>";
            html += "<table class='party-special-detail-table' border = '1'  >";
            html += "<tr>";
            html += "<td align = 'center' width = '25%'>分类名称</td>";
            html += "<td align = 'center' width = '15%' >排序</td>";
            html += "<td align = 'center' width = '15%' >状态</td>";
            html += "<td align = 'center'  width = '15%' >操作</td>";
            html += "</tr>";
            $.ajax({
                type:"POST",
                url:t.path+"/busShopType/getShopTypeByParent.do",
                dataType:"json",
                data:{
                    stParentId:row.stId
                },
                async: false,
                cache:false,
                success:function(data){
                    if(data && data.rows){
                        for (var i = 0; i < data.rows.length; i++) {
                            html += "<tr>";
                            html += "<td align = 'center'>" + data.rows[i].stName + "</td>";
                            html += "<td align = 'center'>" + data.rows[i].stSort + "</td>";
                            if(data.rows[i].stStatus==1){
                                html += "<td align = 'center'>正常</td>";
                            }else{
                                html += "<td align = 'center'><font style='color:red;'>停用</font></td>";
                            }
                            html += '<td align = \'center\'><a href="javascript:ShopType.updateShopTypeDialog(\'' + data.rows[i].stId + '\')" class="btn btn-default btn-xs">编辑</a>&nbsp; <a href="javascript:ShopType.deleteShopTypeDialog(\'' + data.rows[i].stId + '\')" class="btn btn-default btn-xs">删除</a></td>';
                            html += "</tr>";
                        }
                    }else{
                        html += "<tr><td colspan='4'>暂无数据</td></tr>";
                    }
                }
            });
            
            return html;
        },
        operator : function(value, row) {
            var html = "";
            html += '<a href="javascript:ShopType.addShopTypeDialog(\'' + value + '\',\''+row.stName+'\')" class="btn btn-default btn-xs">添加子菜单</a>&nbsp;';
            html += '<a href="javascript:ShopType.updateShopTypeDialog(\'' + value + '\')" class="btn btn-default btn-xs">编辑</a>&nbsp;';
            html += '<a href="javascript:ShopType.deldteShopTypeDialog(\'' + value + '\')" class="btn btn-default btn-xs">删除</a>';
            return html;
        }
    }
    return t;
}();